﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CenterAndRadius
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //接收结果
            string Text;
            //接收三点坐标
            string[] a = textBox1.Text.Split(',', '，');
            string[] b = textBox2.Text.Split(',', '，');
            string[] c = textBox3.Text.Split(',', '，');

            if (a.Length != 2 || b.Length != 2 || c.Length != 2)
            {
                textBox4.Text = "坐标格式错误，请重新输入坐标！！";
                return;
            }
            //坐标装到集合中循环遍历验证格式是否正确，不正确重新输入坐标
            ArrayList array = new ArrayList();
            array.Add(a[0]);
            array.Add(a[1]);
            array.Add(b[0]);
            array.Add(b[1]);
            array.Add(c[0]);
            array.Add(c[1]);
            foreach (string item in array)
            {
                if (RegularExpression(item) != true)
                {
                    textBox4.Text = "坐标格式错误，请重新输入坐标！！";
                    return;
                }
            }
            //转换成double类型
            double a1 = Convert.ToDouble(a[0]);
            double a2 = Convert.ToDouble(a[1]);
            double b1 = Convert.ToDouble(b[0]);
            double b2 = Convert.ToDouble(b[1]);
            double c1 = Convert.ToDouble(c[0]);
            double c2 = Convert.ToDouble(c[1]);
            //计算三边的长度
            //ab的边长
            double ab = Math.Sqrt((a1 - b1) * (a1 - b1) + (a2 - b2) * (a2 - b2));
            //ac的边长
            double ac = Math.Sqrt((a1 - c1) * (a1 - c1) + (a2 - c2) * (a2 - c2));
            //bc的边长
            double bc = Math.Sqrt((c1 - b1) * (c1 - b1) + (c2 - b2) * (c2 - b2));
            //判断是否为三角形
            if (Slope(a1, a2, b1, b2, c1, c2))
            {
                Text = "是三角形！！\r\n";
                //内切圆半径
                RadiusOfInscribedCircle(ab, bc, ac);
                //内切圆圆心
                CenterOfInscribedCircle(a1, a2, b1, b2, c1, c2);
                //外切圆半径
                CircumscribedRadius(ab, bc, ac);
                //外切圆半径(2)
                CR(ab, bc, ac);
                //外切圆圆心
                CenterOfCircumscribedCircle(a1, a2, b1, b2, c1, c2);
                textBox4.Text = Text;
            }
            else
            {
                textBox4.Text = "不是三角形！！！请重新输入坐标！！";
                return;
            }
            //利用面积计算外切圆的半径
            void CircumscribedRadius(double AB, double BC, double AC)
            {
                //计算三角形面积
                double S = Area(AB, BC, AC);
                //(ab*bc*ac)/4S = R;
                double CircumscribedCircleRadius = (AB * BC * AC) / (4 * S);
                Text = Text + "外接圆的半径是:" + CircumscribedCircleRadius;
            }
            //利用正弦定理计算外切圆的半径
            void CR(double AB, double BC, double AC)
            {
                //求CosA
                double CosA = ((BC * BC) + (AC * AC) - (AB * AB)) / (2 * (BC * AC));
                Console.WriteLine("CosA");
                //利用CosA求出SinA代入公式，外切圆半径 = SinA / A = 2R；
                double R = AB / (2 * Math.Sin(Math.Acos(CosA)));
                Text = Text + "外接圆的半径是:" + R + "\r\n";
            }
            //外切圆圆心
            void CenterOfCircumscribedCircle(double ax, double ay, double bx, double by, double cx, double cy)
            {
                //斜率
                double abk = (by - ay) / (bx - ax);
                Console.WriteLine("abk得斜率是{0}", abk);
                double bck = (cy - by) / (cx - bx);
                Console.WriteLine("bck得斜率是{0}", bck);
                double ack = (cy - ay) / (cx - ax);
                Console.WriteLine("ack得斜率是{0}", ack);
                //三条线段的中点
                double abx = (ax + bx) / 2;
                double aby = (ay + by) / 2;
                double bcx = (cx + bx) / 2;
                double bcy = (cy + by) / 2;
                double acx = (cx + ax) / 2;
                double acy = (cy + ay) / 2;
                Console.WriteLine("中心点分别为：ab:{0}，{1}；bc:{2}，{3}；ac:{4}，{5}；", abx, aby, bcx, bcy, acx, acy);
                //三边垂直平分线的斜率
                double abperk, bcperk, acperk;
                if (!(abk == 0))
                {
                    abperk = -1 / abk;
                    Console.WriteLine("k1的斜率是{0}", abperk);
                }
                else
                {
                    abperk = 0;
                    Console.WriteLine("k1线段的斜率:{0}无意义", abk);
                }

                if (!(bck == 0))
                {
                    bcperk = -1 / bck;
                    Console.WriteLine("k2的斜率是{0}", bcperk);
                }
                else
                {
                    bcperk = 0;
                    Console.WriteLine("k2线段的斜率:{0}无意义", bck);
                }

                if (!(ack == 0))
                {
                    acperk = -1 / ack;
                    Console.WriteLine("k3的斜率是{0}", acperk);
                }
                else
                {
                    acperk = 0;
                    Console.WriteLine("k3线段的斜率:{0}无意义", ack);
                }
                //求B值
                double B1 = aby - (abperk * abx);
                Console.WriteLine("B1的值为：{0}", B1);
                double B2 = bcy - (bcperk * bcx);
                Console.WriteLine("B2的值为：{0}", B2);
                double B3 = acy - (acperk * acx);
                Console.WriteLine("B3的值为：{0}", B3);
                //圆心坐标
                double x;
                double y;
                if (!(abperk == 0) && !(bcperk == 0))
                {
                    x = (B2 - B1) / (abperk - bcperk);
                    y = (bcperk * x) + B2;
                    Text = Text + "外接圆圆心的坐标是:" + x + "," + y;
                    Console.WriteLine("外接圆圆心的坐标是：{0},{1}", x, y);
                }
                else if (!(bcperk == 0) && !(acperk == 0))
                {
                    x = (B3 - B2) / (bcperk - acperk);
                    y = (acperk * x) + B3;
                    Text = Text + "外接圆圆心的坐标是:" + x + "," + y;
                    Console.WriteLine("外接圆圆心的坐标是：{0},{1}", x, y);
                }
                else if (!(abperk == 0) && !(acperk == 0))
                {
                    x = (B3 - B1) / (abperk - acperk);
                    y = (abperk * x) + B1;
                    Text = Text + "外接圆圆心的坐标是:" + x + "," + y;
                    Console.WriteLine("外接圆圆心的坐标是：{0},{1}", x, y);
                }
                else if ((abperk == 0) & (bcperk == 0))
                {
                    x = acx;
                    y = acy;
                    Text = Text + "外接圆圆心的坐标是:" + x + "," + y;
                    Console.WriteLine("外接圆圆心的坐标是：{0},{1}", x, y);
                }
                else if ((bcperk == 0) & (acperk == 0))
                {
                    x = abx;
                    y = aby;
                    Text = Text + "外接圆圆心的坐标是:" + x + "," + y;
                    Console.WriteLine("外接圆圆心的坐标是：{0},{1}", x, y);
                }
                else if ((abperk == 0) & (acperk == 0))
                {
                    x = bcx;
                    y = bcy;
                    Text = Text + "外接圆圆心的坐标是:" + x + "," + y;
                    Console.WriteLine("外接圆圆心的坐标是：{0},{1}", x, y);
                }
            }
            //计算内切圆的圆心坐标
            void CenterOfInscribedCircle(double ax, double ay, double bx, double by, double cx, double cy)
            {
                //内心坐标为((ax1+bx2+cx3)/(a+b+c),(ay1+by2+cy3)/(a+b+c))
                double InscribedCircleCoordinateX = ((bc * ax) + (ac * bx) + (ab * cx)) / (ab + bc + ac);
                double InscribedCircleCoordinateY = ((bc * ay) + (ac * by) + (ab * cy)) / (ab + bc + ac);
                Text = Text + "内切圆圆心的坐标是:" + InscribedCircleCoordinateX + "," + InscribedCircleCoordinateY + "\r\n";
            }
            //利用面积计算内切圆的半径
            void RadiusOfInscribedCircle(double AB, double BC, double AC)
            {
                //求面积
                double S = Area(AB, BC, AC);
                //求内切圆半径
                double RadiusInscribedCircle = (2 * S) / (AB + BC + AC);
                Text = Text + "内切圆的半径为" + RadiusInscribedCircle + "\r\n";
            }
            //计算面积，利用周长求面积
            double Area(double AB, double BC, double AC)
            {
                //三角形的周长
                double Perimeter = AB + BC + AC;
                //半周长
                double HalfCircumference = Perimeter * 0.5;
                //三角形的面积
                double S = Math.Sqrt(HalfCircumference * (HalfCircumference - AB) * (HalfCircumference - BC) * (HalfCircumference - AC));
                return S;
            }
            ////判断三点(a1,a2)(b1,b2)(c1,c2)是否共线，return 0代表在同一条直线上
            bool Slope(double ax, double ay, double bx, double by, double cx, double cy)
            {
                //判断三点x轴是否在一条直线上，若不在一条直线上可以组成三角形；则不能组成三角形
                if (bx != ax && cx != bx && cx != ax)
                {
                    //计算三边的斜率
                    double abk = (by - ay) / (bx - ax);
                    double bck = (cy - by) / (cx - bx);
                    double ack = (cy - ay) / (cx - ax);
                    //判断三边斜率是否相同，若相同不能组成三角形，则可以组成三角形；
                    if (abk == bck && bck == ack)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                //判断三点坐标的x值是否同一个数，若相同不能组成三角形，则可以组成三角形；
                else if (ax == bx && bx == cx)
                {
                    return false;
                }
                else
                {
                    //判断三点坐标y值是否同一个数，若相同不能组成三角形，则可以组成三角形；
                    if (ay == by && by == cy)
                        return false;
                    else
                        return true;
                }
            }
            //判断文本框出入的格式是否正确；
            bool RegularExpression(string y)
            {
                bool Expression = Regex.IsMatch(y, @"(^-?[0-9]{1,}$)|(^-?[0-9]{1,}\.{1}[0-9]{1,}$)");
                return Expression;
            }
        }
    }
}
